class TimeUtil {
  /// 获取现在日期时间
  static String nowTime() {
    String nowTime = DateTime.now().toString();
    return nowTime = nowTime.substring(0, nowTime.lastIndexOf("."));
  }

  /// 获取现在时间戳，10位，不含毫秒
  static int nowTimeMilliseconds() {
    return DateTime.now().millisecondsSinceEpoch ~/ 1000;
  }

  /// 不含毫秒的时间戳转时间
  static String millisecondsToTime(int milliseconds) {
    String time =
        DateTime.fromMillisecondsSinceEpoch(milliseconds * 1000).toString();
    return time = time.substring(0, time.lastIndexOf("."));
  }

  /// 获取现在日期
  static String nowDate() {
    String nowTime = DateTime.now().toString();
    return nowTime = nowTime.substring(0, nowTime.lastIndexOf(" "));
  }

  /// 时间转时间戳
  static int dateToTimeMilliseconds(String? date) {
    if (date == null || date.isEmpty) {
      return 0;
    }
    return DateTime.parse(date).millisecondsSinceEpoch ~/ 1000;
  }

  /// 时间截取 yyyy-MM-dd hh:mm
  /// date : yyyy-MM-dd hh:mm:ss
  static String time2yyyyMMddhhmm(String? date) {
    if (date == null || date.isEmpty) {
      return '';
    }
    return date.substring(0, date.length - 3);
  }

  /// 时间数据格式化方法
  /// timeData : yyyy-MM-dd hh:mm:ss
  /// timeFormat
  /// yyyy-MM-dd hh:mm
  /// yyyy-MM-dd hh
  /// yyyy-MM-dd
  /// yyyy-MM
  /// yyyy
  static String timeFormat(String timeData, String timeFormat) {
    String tempTime = timeData;
    if (timeData == 'yyyy-MM-dd hh:mm:ss') {
      return tempTime;
    }
    if (timeFormat == 'yyyy-MM-dd hh:mm') {
      return tempTime.substring(0, 16);
    }
    if (timeFormat == 'yyyy-MM-dd hh') {
      return tempTime.substring(0, 13);
    }
    if (timeFormat == 'yyyy-MM-dd') {
      return tempTime.substring(0, 10);
    }
    if (timeFormat == 'yyyy-MM') {
      return tempTime.substring(0, 7);
    }
    if (timeFormat == 'yyyy') {
      return tempTime.substring(0, 4);
    }
    if (timeFormat == 'hh:mm') {
      return tempTime.substring(11, 16);
    }
    return tempTime;
  }

  /// 年
  static List<int> calcYear({int begin = 1900, int end = 2100}) =>
      _calcCount(begin, end);

  /// 月
  static List<int> calcMonth({int begin = 1, int end = 12}) {
    begin = begin < 1 ? 1 : begin;
    end = end > 12 ? 12 : end;
    return _calcCount(begin, end);
  }

  /// 日
  static List<int> calcDay(int year, int month, {int begin = 1, int end = 31}) {
    begin = begin < 1 ? 1 : begin;

    int days = calcDateCount(year, month);
    if (end > days) {
      end = days;
    }
    return _calcCount(begin, end);
  }

  /// 时
  static List<int> calcHour({int begin = 0, int end = 23}) {
    begin = begin < 0 ? 0 : begin;
    end = end > 23 ? 23 : end;
    return _calcCount(begin, end);
  }

  /// 分 和 秒
  static List<int> calcMinAndSecond({int begin = 0, int end = 59}) {
    begin = begin < 0 ? 0 : begin;
    end = end > 59 ? 59 : end;
    return _calcCount(begin, end);
  }

  static List<int> _calcCount(begin, end) {
    int length = end - begin + 1;
    if (length == 0) return [begin];
    if (length < 0) return [];

    return List.generate(length, (index) => begin + index);
  }

  // 计算月份所对应天数
  static int calcDateCount(int year, int month) {
    switch (month) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        return 31;
      case 2:
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
          return 29;
        }
        return 28;
    }
    return 30;
  }
}
